<!-- 模板 -->
<template>
  <div class="login-root">
    <div class="box center-ps">
      <!-- 1、logo -->
      <div class="logo-box bd center-fx">
        <img src="../assets/logo.jpg" alt="" />
      </div>
      <!-- 2、登录表单 -->
      <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef" label-width="0.6rem" class="loginForm">
        <!-- 
          el-form:表单组件
          :model="loginForm" 表单双向绑定的数据源
          :rules="loginFormRules" 表单验证的规则
          ref="loginFormRef" 表单的引用,可以通过this.$refs.loginFormRef找到此表单元素
          label-width="100px" ‘用户’ ‘密码’的宽度
         -->
        <el-form-item label="用户" prop="username">
          <!-- prop:表单验证的username字段 -->
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm()">登录</el-button>
          <el-button @click="resetForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import request from '../utils/request.js'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      //登录表单的验证规则
      loginFormRules: {
        //对username的验证规则
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        //对password的验证规则
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async submitForm() {
      //找到表单元素
      let form = this.$refs['loginFormRef']
      console.log(form)
      //表单验证 valid是验证的结果
      form.validate(async valid => {
        console.log(valid)
        if (!valid) {
          //this.$message 自带的信息弹窗
          return this.$message.error('请正确填写用户信息')
        }
        let option = {
          type: 'post',
          url: 'login',
          data: this.loginForm
        }
        //发送请求
        let { data: res } = await request(option)
        console.log(res)
        if (res.meta.status !== 200) {
          //this.$message 自带的信息弹窗
          return this.$message.error(res.meta.msg)
        }
        //登录成功
        //this.$message 自带的信息弹窗
        this.$message.success(res.meta.msg)
        sessionStorage.setItem('USER_LOGIN', JSON.stringify(res.data))
        this.$router.push('/welcome')
      })
    },
    resetForm() {}
  },

  created() {},
  mounted() {}
}
</script>

<style lang="less" scoped>
.login-root {
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background-color: #2c3e50;
  .box {
    width: 5rem;
    height: 3.2rem;
    background-color: #fff;
    border-radius: 0.1rem;
    .logo-box {
      height: 1.2rem;
      width: 1.2rem;
      border-radius: 50%;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      border-color: #ddd;
      background-color: #fff;
      img {
        width: 100%;
        height: 1rem;
        width: 1rem;
        border-radius: 50%;
      }
    }
    .loginForm {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0.1rem;
      .btns {
        text-align: right;
      }
    }
  }
}
</style>
